﻿using EkStudio.com.bean.xml.reflection;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.RootInterface.Field;
using System;
using System.Collections.Generic;

namespace EkStudio.com.bean.xml.BillModule
{
    public class BillUiGroupRoot : BillUiFormatContainer
    {

        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(BillUiGroupRoot));

        private int maxid = 0;
        private List<BillUiGroupItems> fieldsProperties;

        [Xml]
        public int Maxid
        {
            get
            {
                return maxid;
            }

            set
            {
                maxid = value;
            }
        }
        [XmlElement("fieldsProperties")]
        public List<BillUiGroupItems> FieldsProperties
        {
            get
            {
                return fieldsProperties;
            }

            set
            {
                fieldsProperties = value;
            }
        }

        public object ParentBean
        {
            get
            {
                return null;
            }

            set
            {

            }
        }

        public object Clone()
        {
            BillUiGroupRoot clone = new BillUiGroupRoot();
            clone.Maxid = 0;
            clone.FieldsProperties = new List<BillUiGroupItems>();
            if (this.fieldsProperties != null && this.fieldsProperties.Count > 0)
            {
                for (int i = 0; i < fieldsProperties.Count; i++)
                {
                    Object item = fieldsProperties[i].Clone();
                    if (item is BillUiGroupItems)
                    {
                        //((BillUiGroupItems)item).setParent(clone);
                        clone.FieldsProperties.Add((BillUiGroupItems)item);
                    }
                }
            }
            return clone;
        }

        public void addNode(BillUiFormatNode node, BillFormatModule format)
        {
            BillUiGroupItems group = node as BillUiGroupItems;
            if (group.Fields == null) group.Fields = new List<BillUiFieldsItem>();
            group.LanguageTexts = getNLanguageText(format);
            if (group.Fields.Count > 0) group.updateLanguageText(format);
            fieldsProperties.Add(group);
        }

        public void removeNode(BillUiFormatNode node)
        {
            BillUiGroupItems group = node as BillUiGroupItems;
            if (this.fieldsProperties.Contains(group))
            {
                this.fieldsProperties.Remove(group);
            }
        }

        public void moveUp(BillUiFormatNode node)
        {
            BillUiGroupItems group = node as BillUiGroupItems;
            int index = this.fieldsProperties.IndexOf(group);
            if (index != -1)
            {
                int PevIndex = index - 1;
                //logger.Info("移动前当前[" + this.fieldsProperties[index].Caption + "] 前位是[" + this.fieldsProperties[PevIndex].Caption + "]");
                
                this.fieldsProperties.RemoveAt(index);
                this.fieldsProperties.Insert(PevIndex, group);
                //logger.Info("移动后当前[" + this.fieldsProperties[index].Caption + "] 前位是[" + this.fieldsProperties[PevIndex].Caption + "]");
            }

        }

        public void moveDown(BillUiFormatNode node)
        {
            BillUiGroupItems group = node as BillUiGroupItems;
            int index = this.fieldsProperties.IndexOf(group);
            if(index < (this.fieldsProperties.Count -1))
            {
                
                int LastIndex = index + 1;
                BillUiGroupItems LastGroup = this.fieldsProperties[LastIndex];
                this.fieldsProperties.RemoveAt(LastIndex);
                this.fieldsProperties.Insert(index, LastGroup);
            }
        }

        public string getNLanguageText(BillFormatModule format)
        {
            String result = "";
            int tempMaxid = maxid + 1;
            if (format != null)
            {
                string text = format.getLanguageText();
                int index = text.IndexOf("Caption");
                int length = text.Length - 7;
                result = text.Substring(0, length);
                result = result + "Groups[" + tempMaxid + "].Caption";
            }
            this.Maxid = tempMaxid;
            return result;
        }

        public void updateLanguageText(BillFormatModule format)
        {
            this.maxid = 0;
            if (this.FieldsProperties != null && this.fieldsProperties.Count > 0)
            {
                foreach(BillUiGroupItems group in this.fieldsProperties)
                {
                    group.LanguageTexts = this.getNLanguageText(format);
                    group.updateLanguageText(format);
                }
            }
        }
    }
}
